package servlets;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import bdd.Connexion;
import bdd.beans.DBLink;
import bdd.beans.Ec;
import bdd.beans.Enseignant;
import bdd.beans.Jnttypeecenseignant;
import bdd.beans.ListeEnseignants;
import bdd.beans.ListeTypes;
import bdd.beans.Type;
import bdddaosimpl.DAOEcImpl;
import bdddaosimpl.DAOEnseignantImpl;
import bdddaosimpl.DAOJnttypeecenseignantImpl;
import bdddaosimpl.DAOTypeImpl;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author maboisba
 */
public class ListeEnseignantsServlet extends HttpServlet {

  @Override
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
          throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    String url = request.getRealPath("WEB-INF/prop.properties");
    out.println("DANS DOGET");
    DAOEnseignantImpl e = new DAOEnseignantImpl();
    List<Enseignant> l = e.findByEnseignantPoste();
    //ListeEnseignants listeEnseignants = new ListeEnseignants();
   //listeEnseignants.recupererListeEnseignants();
    //request.setAttribute("listeEnseignants", listeEnseignants);
   

    request.setAttribute("listeEnseignants", l);

    // On re-route l'appel vers la page d'accueil JSP
    gotoPage("/gestion_ec.jsp", request, response);
  }

  @Override
  protected void doPost(HttpServletRequest request, HttpServletResponse response)
          throws ServletException, IOException {
        try {
            response.setContentType("text/html;charset=UTF-8");
            DBLink db = new DBLink();
            db.connexion();
            PrintWriter out = response.getWriter();
            int numeroEns;
            int numeroType;
            int numeroEc;
            int nbHeures;
            if(request.getParameter("valider").equals("ajouter")){
                numeroEns = Integer.parseInt(request.getParameter("numero_enseignant"));
                numeroType = Integer.parseInt(request.getParameter("types"));
                numeroEc = Integer.parseInt(request.getParameter("ecs"));
                nbHeures = Integer.parseInt(request.getParameter("nbHeures"));
                out.println(numeroEns + " " + numeroType + " " + numeroEc + " " + nbHeures);
                String rq = "SELECT JNTTYPEECENSEIGNANT.numenseignant, JNTTYPEECENSEIGNANT.numtype, JNTTYPEECENSEIGNANT.numec, "
                        + "JNTTYPEECENSEIGNANT.nbheures as nbheurestypeec FROM JNTTYPEECENSEIGNANT WHERE "
                        + "JNTTYPEECENSEIGNANT.numenseignant = ? AND JNTTYPEECENSEIGNANT.numtype = ? "
                        + "AND JNTTYPEECENSEIGNANT.numec = ?";
                PreparedStatement pst = db.getConnection().prepareStatement(rq);
                pst.setInt(1, numeroEns);
                pst.setInt(2, numeroType);
                pst.setInt(3, numeroEc);
                db.setPst(pst);
                ResultSet rs = db.getPst().executeQuery();
                boolean trouve = false;
                while(rs.next()){
                    trouve = true;
                    int heures = rs.getInt("nbheurestypeec");
                    out.println("resultat trouve " + rs.getInt("nbheurestypeec"));
                    break;
                }
                if(trouve == false){
                    DAOJnttypeecenseignantImpl daoJnt = new DAOJnttypeecenseignantImpl();
                    Type type = new Type();
                    type.setNumtype(numeroType);
                    Enseignant enseignant = new Enseignant();
                    enseignant.setNumenseignant(numeroEns);
                    Ec ec = new Ec();
                    ec.setNumec(numeroEc);
                    Jnttypeecenseignant jnt = new Jnttypeecenseignant(21, type, enseignant, ec, nbHeures);
                    daoJnt.save(jnt);
                    request.setAttribute("ajoute", "oui");
                }else{
                    request.setAttribute("ajoute", "non");
                }
            }else if(request.getParameter("sup").equals("Supprimer")){
                 numeroEns = Integer.parseInt(request.getParameter("numEns"));
                 numeroEc = Integer.parseInt(request.getParameter("numEc"));
                 numeroType = Integer.parseInt(request.getParameter("numType"));
                 DAOJnttypeecenseignantImpl daoJnt = new DAOJnttypeecenseignantImpl();
                    Type type = new Type();
                    type.setNumtype(numeroType);
                    Enseignant enseignant = new Enseignant();
                    enseignant.setNumenseignant(numeroEns);
                    Ec ec = new Ec();
                    ec.setNumec(numeroEc);
                    Jnttypeecenseignant jnt = new Jnttypeecenseignant(21, type, enseignant, ec);
                    daoJnt.delete(jnt);
            }
               int num_enseignant;
             num_enseignant = Integer.parseInt(request.getParameter("numero_enseignant"));
        request.setAttribute("numeroEnseignant", num_enseignant);

        DAOEnseignantImpl dao = new DAOEnseignantImpl();
        List<Enseignant> l = dao.findByEnseignantPoste();
        request.setAttribute("listeEnseignants", l);

        DAOEnseignantImpl uneDao=new DAOEnseignantImpl();
        Enseignant unEns=uneDao.find(num_enseignant);
        Set<Jnttypeecenseignant> listeJnt= unEns.getJnttypeecenseignants();
        request.setAttribute("listeJnt", listeJnt);


        DAOTypeImpl daoType = new DAOTypeImpl();
        List<Type> listeTypes = daoType.findAll();
        request.setAttribute("listeTypes", listeTypes);
        /*ListeTypes listeTypes = new ListeTypes();
        listeTypes.recupererListeTypes();
        request.setAttribute("listeTypes", listeTypes.getListeTypes());*/

        DAOEcImpl daoEc = new DAOEcImpl();
        List<Ec> listeEC = daoEc.findAll();
        //ListeEC listeEC = new ListeEC();
        //listeEC.recupererListeEC();
        request.setAttribute("listeEC", listeEC);

        Integer a = 1;
        request.setAttribute("number", a);

            db.getConnection().close();
            gotoPage("/gestion_ec.jsp", request, response);
        } catch (SQLException ex) {
            Logger.getLogger(ListeEnseignantsServlet.class.getName()).log(Level.SEVERE, null, ex);
        }
  }

  private void gotoPage(String adress, HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
    RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(adress);
    dispatcher.forward(request,response);
  }

}
